package com.offroader.pay.weichat;

import android.app.Activity;

import com.offroader.R;
import com.offroader.utils.ViewUtils;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WeicahtPayHelper {
	public static final WeicahtPayHelper instance = new WeicahtPayHelper();

	private WeicahtPayHelper() {
	}

	public static WeicahtPayHelper getInstance() {
		return instance;
	}

	public void pay(Activity act, WXPayOrder orderInfo) {

		if (orderInfo == null) {
			return;
		}

		//1.构造PayReq 对象
		PayReq payReq = payOrder(orderInfo);

		//2.调用支付接口
		IWXAPI mWeixinAPI = WXAPIFactory.createWXAPI(act, orderInfo.getAppid(), false);

		if (!mWeixinAPI.isWXAppInstalled()) {
			ViewUtils.toast(R.string.tip_wechat_install);
		}

		mWeixinAPI.registerApp(orderInfo.getAppid());
		mWeixinAPI.sendReq(payReq);
	}

	/**
	 * 调起本地微信支付，支付订单.
	 * 
	 * @author taoyf
	 * @time 2015年3月31日
	 */
	private PayReq payOrder(WXPayOrder payorder) {

		PayReq payReq = new PayReq();
		payReq.appId = payorder.getAppid();
		payReq.partnerId = payorder.getPartnerid();
		payReq.prepayId = payorder.getPrepayid();
		payReq.packageValue = payorder.getPackageStr();
		payReq.nonceStr = payorder.getNoncestr();
		payReq.timeStamp = payorder.getTimestamp();
		payReq.sign = payorder.getSign();

		return payReq;

	}

}
